// PostWithTagsRequest 创建带标签的帖子请求
type PostWithTagsRequest struct {
	Title   string     `json:"title"`
	Content string     `json:"content"`
	Tags    []string   `json:"tags"`
	Images  []ImageData `json:"images"` // 添加图片字段
}

// CreatePostWithTags 创建带标签的帖子
func CreatePostWithTags(req PostWithTagsRequest, userID uint) (*models.Post, error) {
	// 创建事务
	tx := models.DB.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	// 创建帖子
	post := models.Post{
		Title:     req.Title,
		Content:   req.Content,
		UserID:    userID,
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
	}
	if err := tx.Create(&post).Error; err != nil {
		tx.Rollback()
		return nil, err
	}

	// 处理标签
	if len(req.Tags) > 0 {
		for _, tagName := range req.Tags {
			// 查找或创建标签
			var tag models.Tag
			if err := tx.Where("name = ?", tagName).FirstOrCreate(&tag, models.Tag{
				Name: tagName,
			}).Error; err != nil {
				tx.Rollback()
				return nil, err
			}

			// 创建帖子与标签的关联
			postTag := models.PostTag{
				PostID: post.ID,
				TagID:  tag.ID,
			}
			if err := tx.Create(&postTag).Error; err != nil {
				tx.Rollback()
				return nil, err
			}
		}
	}

	// 处理图片上传
	if len(req.Images) > 0 {
		_, err := SavePostImages(req.Images, post.ID)
		if err != nil {
			tx.Rollback()
			return nil, err
		}
	}

	// 提交事务
	if err := tx.Commit().Error; err != nil {
		return nil, err
	}

	return &post, nil
}

// GetPostDetail 获取帖子详情
func GetPostDetail(id uint) (*PostDetail, error) {
	var post models.Post
	if err := models.DB.First(&post, id).Error; err != nil {
		return nil, err
	}

	var user models.User
	if err := models.DB.Select("id, username").First(&user, post.UserID).Error; err != nil {
		return nil, err
	}

	var comments []CommentDetail
	if err := models.DB.Table("comments").
		Select("comments.id, comments.content, comments.created_at, users.username, comments.user_id").
		Joins("left join users on comments.user_id = users.id").
		Where("comments.post_id = ?", id).
		Order("comments.created_at desc").
		Find(&comments).Error; err != nil {
		return nil, err
	}

	// 获取帖子标签
	var tags []models.Tag
	if err := models.DB.Table("tags").
		Joins("inner join post_tags on tags.id = post_tags.tag_id").
		Where("post_tags.post_id = ?", id).
		Find(&tags).Error; err != nil {
		return nil, err
	}

	// 获取帖子图片
	images, err := GetPostImages(id)
	if err != nil {
		return nil, err
	}

	return &PostDetail{
		ID:        post.ID,
		Title:     post.Title,
		Content:   post.Content,
		CreatedAt: post.CreatedAt,
		UpdatedAt: post.UpdatedAt,
		User: UserInfo{
			ID:       user.ID,
			Username: user.Username,
		},
		Comments: comments,
		Tags:     tags,
		Images:   images, // 添加图片信息
	}, nil
}

// PostDetail 帖子详情
type PostDetail struct {
	ID        uint             `json:"id"`
	Title     string           `json:"title"`
	Content   string           `json:"content"`
	CreatedAt time.Time        `json:"created_at"`
	UpdatedAt time.Time        `json:"updated_at"`
	User      UserInfo         `json:"user"`
	Comments  []CommentDetail  `json:"comments"`
	Tags      []models.Tag     `json:"tags"`
	Images    []models.PostImage `json:"images"` // 添加图片字段
} 